package restless.aimovement.astar.analyzers {
	
	import flash.geom.Point;
	import restless.aimovement.astar.Analyzer;
	import restless.aimovement.astar.interfaces.IPositionTile;
	
	/**
	 * @author MNS
	 */
	
	 public class ThreeWayMovementAnalyzer extends Analyzer {
		
		protected var currentDirection:int;
		protected var previousTile:IPositionTile;
		protected var bFristTile:Boolean = true;
		
		protected const UP:int = 1;
		protected const DOWN:int = 2;
		protected const LEFT:int = 3;
		protected const RIGHT:int = 4;
		protected const STOP:int = 0;
		
		public function ThreeWayMovementAnalyzer(dir:int) {
			currentDirection = dir;
			super();
		}
		
		override public function getTileInterface():Class {
			return ThreeWayMovementAnalyzer;	
		}
		
		//main tile = center tile
		override protected function analyze(mainTile:*, allNeighbours:Array, neighboursLeft:Array, map:IMap):Array {
			var center:IPositionTile = mainTile;
			var centerTilePoint:Point = center.position;
			var currentTile:IPositionTile
			var newLeft:Array = [];
			var pCurrentTiile:Point
			var bSkipTile:Boolean = false;
			var pPrevTile:Point;
			var i:Number = 0;
			
			for (i; i < neighboursLeft.length; i++)	{
				currentTile = neighboursLeft[i];
				pCurrentTiile = currentTile.position;
				bSkipTile = false;
				
				if (!bFristTile) {
					pPrevTile = previousTile.position;
					if (pPrevTile.x > centerTilePoint.x) {
						currentDirection = RIGHT;
					} else if (pPrevTile.x < centerTilePoint.x) {
						currentDirection = LEFT;
					} else if (pPrevTile.y > centerTilePoint.y) {
						currentDirection = DOWN;
					} else if (pPrevTile.y < centerTilePoint.y) {
						currentDirection = UP;
					} 
				}
				bFristTile = false;
				previousTile = mainTile;
				
				switch (currentDirection) {
					case RIGHT:
						if (pCurrentTiile.x < centerTilePoint.x) {
							newLeft.push(currentTile);
							break;
						}
					
					case LEFT:  
						if (pCurrentTiile.x > centerTilePoint.x) {
							newLeft.push(currentTile);
							break;
						} 
					
					case UP:
						if (pCurrentTiile.y > centerTilePoint.y) {
							newLeft.push(currentTile);
							break;
						}
					
					case DOWN:  
						if (pCurrentTiile.y < centerTilePoint.y) {
							newLeft.push(currentTile);
							break;
						}
				}
			}
			
			return newLeft;
		}
	}
}